package GDoc::Job::Fetch::US::Legi::GAO::Reports;

use Moose;
use WWW::Mechanize;
use File::Temp;

use GDoc::Job;


extends 'GDoc::Job::Fetch::US::Legi::GAO';

sub setup {
    my $self = shift;
    $self->_baseurl( 'http://www.gao.gov/docsearch/date.php' );
}

sub run { 
    my $self = shift;
    my %data = %{ $self->data };

    if ( $data{link} ) { 
        $self->_fetch_pdf;
    } else { 
        $self->_fetch_search;
    }
}


sub _fetch_search { 
    my $self = shift;

    my $mech = $self->_mech;

    # GAO reports from before 2001 have a different numbering system
    # we'll deal with those later
    foreach my $syear ( 2001 .. 2009 ) { 
        $self->logger->info( "Fetching base " . $self->_baseurl );
        $mech->get( $self->_baseurl );
        $self->logger->info( "Submitting search form for year $syear" );
        $mech->submit_form( with_fields => { date1_day      => 1,
                                             date1_month    => 1,
                                             date1_year     => $syear,
                                             date2_day      => 31,
                                             date2_month    => 12,
                                             date2_year     => $syear
                                           }
                          );
        
        while( 1 ) { 
            my $html = $mech->response->content;
            my @results = ( $html =~ m{<dl class='scannableList'>\s+(<dt class='resultTitle'>.*?)</dl>}gsi );

            foreach my $res( @results ) { 
                $res =~ m{<strong>(?<subj>.+?)</strong>:\s+(?<title>.+?)</dt>}si;
                next unless $+{subj} and $+{title};

                my $subj = $+{subj};
                my $title = $+{title};

                $res =~ m{<a\s+href="(?<url>http://www\.gao\.gov/(.*?)\.pdf)">(?<num>GAO-\d{2}-\d+[TR]?)</a>};
                next unless $+{url} and $+{num};

                my $url = $+{url};
                my $num = $+{num};

                my $job = GDoc::Job->new( class => 'Fetch::US::Legi::GAO::Reports',
                                          queue => $self->fetch_queue,
                                          data  => { subj    => $subj,
                                                     link    => $url,
                                                     title   => $title,
                                                     num     => $num
                                                   }
                                        );

                $job->submit;
            }

            # keep clicking Next until done
            my $nlink = $mech->find_link( url_regex  => qr{page=\d+$},
                                          text_regex => qr{^Next$} );

            last unless $nlink;
            sleep 1;
            $mech->get( $nlink );
        }
    }
}


sub _fetch_pdf { 
    my $self = shift;
    my %data = %{ $self->data };

    my $num = $data{num};
    $num =~ /GAO-(?<year2>\d{2})-(?<suffnum>\d+)(?<suffcode>[TR]?)$/;
    my $year2     = $+{year2};
    my $suffnum   = $+{suffnum};
    my $suffcode  = $+{suffcode} || '';

    # assume all of these are post-y2k
    my $year = 2000 + $year2;
    my $type = ( $suffcode eq 'T' ) ? 'testimony'
             : ( $suffcode eq 'R' ) ? 'correspondence'
             : 'reports';

    my $ident    = sprintf( 'GAO-%04d-%04d%s', $year, $suffnum, $suffcode );
    my $filename = $ident . '.pdf';
    my $key      = sprintf( 'us/legi/gao/%s/%s', $type, $filename );

    my $bkt = $self->s3->bucket( $self->docs_bucket );
    if ( $bkt->head_key( $key ) ) { 
        $self->logger->info( "Already have $data{link}, skipping" );
        return;
    }

    $self->logger->info( "Storing PDF $data{link} --> $key" );

    my $mech = $self->_mech;
    my $tmpf = File::Temp->new;
    $mech->get( $data{link}, ':content_file' => $tmpf->filename );

    $self->store_pdf( key      => $key,
                      datafile => $tmpf,
                      filename => $filename,
                      meta     => { description   => sprintf( '%s: %s', @data{'subj', 'title'} ),
                                    identifier    => $ident,
                                    subject       => $data{subj}
                                  }
                    );
}

1;
